package com.thb.pay.manage.comm.aspect;

import com.thb.pay.core.annotation.PermissionRequired;
import com.thb.pay.core.enums.Action;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @Description 登入权限aop
 * @Author lucky
 * @data 2019/7/28 21:06
 */
@Slf4j
@Order(3)
@Component
@Aspect
public class PermissionAspect {

    public static PermissionRequired getRequired(ProceedingJoinPoint joinPoint) throws Exception {
        Signature sig = joinPoint.getSignature();
        Method m = joinPoint.getTarget().getClass().getMethod(joinPoint.getSignature().getName(), ((MethodSignature) sig).getParameterTypes());
        PermissionRequired required = m.getAnnotation(PermissionRequired.class);
        return required;
    }

    /**
     * 切点
     */
    @Pointcut("@annotation(com.thb.pay.core.annotation.PermissionRequired)")
    public void permissionPointcut() {
    }

    /**
     * 切面
     *
     * @param point
     * @return
     * @throws Throwable
     */
    @Around("permissionPointcut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        checkPermission(point);
        Object result = point.proceed();
        return result;
    }

    public void checkPermission(ProceedingJoinPoint point) throws Throwable {
        PermissionRequired required = getRequired(point);
        if (required != null && required.requrid().equals(Action.Normal)) {
            //
        }
    }

}
